<template>
  <div class="app-container">
    <div v-if="!open && !isView"
         class="body">
      <el-form :model="queryParams"
               ref="queryForm"
               size="small"
               :inline="true"
               v-show="showSearch"
               label-width="100px">
        <el-form-item label="留言标题"
                      prop="messageTitle">
          <el-input v-model="queryParams.messageTitle"
                    placeholder="请输入留言标题"
                    clearable
                    @keyup.enter.native="handleQuery" />
        </el-form-item>

        <el-form-item label="留言类型"
                      prop="messageType">
          <el-select v-model="queryParams.messageType"
                     placeholder="留言类型"
                     clearable>
            <el-option v-for="dict in dict.type.sys_message_type"
                       :key="dict.value"
                       :label="dict.label"
                       :value="dict.value" />
          </el-select>
        </el-form-item>

        <el-form-item label="留言状态"
                      prop="messageStatus">
          <el-select v-model="queryParams.messageStatus"
                     placeholder="留言状态"
                     clearable>
            <el-option v-for="dict in dict.type.sys_message_status"
                       :key="dict.value"
                       :label="dict.label"
                       :value="dict.value" />
          </el-select>
        </el-form-item>


        <el-form-item>
          <el-button type="primary"
                     icon="el-icon-search"
                     size="mini"
                     @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh"
                     size="mini"
                     @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-row :gutter="10"
              class="mb8">
        <el-col :span="1.5">
          <el-button type="primary"
                     plain
                     icon="el-icon-plus"
                     size="mini"
                     @click="handleAdd"
                     v-hasPermi="['operate:message:add']">新增</el-button>
        </el-col>
        <!-- <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['operate:message:edit']"
          >修改</el-button>
        </el-col> -->
        <el-col :span="1.5">
          <el-button type="danger"
                     plain
                     icon="el-icon-delete"
                     size="mini"
                     :disabled="multiple"
                     @click="handleDelete"
                     v-hasPermi="['operate:message:remove']">删除</el-button>
        </el-col>

        <right-toolbar :showSearch.sync="showSearch"
                       @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table v-loading="loading"
                :data="messageList"
                height="calc(100vh - 280px)"
                @selection-change="handleSelectionChange">
        <el-table-column type="selection"
                         width="55"
                         align="center" />
        <el-table-column label="序号"
                         align="center"
                         show-overflow-tooltip
                         prop="id"
                         width="50" />
        <el-table-column label="留言标题"
                         align="center"
                         prop="messageTitle"
                         width="240"
                         :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <span style="color:#aa2290;">{{ scope.row.messageTitle }}</span>
          </template>
        </el-table-column>
        <el-table-column label="留言类型"
                         align="center"
                         show-overflow-tooltip
                         prop="messageType"
                         width="120">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.sys_message_type"
                      :value="scope.row.messageType" />
          </template>
        </el-table-column>
        <el-table-column label="留言状态"
                         align="center"
                         show-overflow-tooltip
                         prop="messageStatus"
                         width="120">
          <template slot-scope="scope">
            <div style="color:#aa2290;">
              <dict-tag :options="dict.type.sys_message_status"
                        :value="scope.row.messageStatus" />
            </div>
          </template>
        </el-table-column>

        <el-table-column label="留言人"
                         align="center"
                         prop="createBy"
                         width="120"
                         :show-overflow-tooltip="true">
        </el-table-column>
        <el-table-column label="留言时间"
                         align="center"
                         show-overflow-tooltip
                         prop="messageTime"
                         >
          <template slot-scope="scope">
            <span>{{ scope.row.messageTime }}</span>
          </template>
        </el-table-column>

        <el-table-column label="回复时间"
                         align="center"
                         show-overflow-tooltip
                         prop="replyTime"
        >
          <template slot-scope="scope">
            <span>{{ scope.row.replyTime }}</span>
          </template>
        </el-table-column>

        <el-table-column label="操作"
                         align="center"
                         class-name="small-padding fixed-width">
          <template slot-scope="scope">
<!--            <el-button v-if="scope.row.messageStatus !='3'"-->
<!--                       size="mini"-->
<!--                       type="text"-->
<!--                       icon="el-icon-edit"-->
<!--                       @click="handleUpdate(scope.row)"-->
<!--                       v-hasPermi="['operate:message:edit']">修改</el-button>-->

            <el-button size="mini"
                       type="text"
                       icon="el-icon-view"
                       @click="handleView(scope.row)"
                       v-hasPermi="['operate:message:query']">查看</el-button>
            <el-button
                       v-if="role == 'admin'"
                       size="mini"
                       type="text"
                       icon="el-icon-delete"
                       @click="handleDelete(scope.row)"
                       v-hasPermi="['operate:message:remove']">删除</el-button>

            <el-button v-if=" (role == 'admin')||(isRegister == 1 && scope.row.messageStatus ==='2') ||  (isRegister != 1 && scope.row.messageStatus ==='1')"
                       size="mini"
                       type="text"
                       icon="el-icon-edit"
                       @click="handleReply(scope.row)"
                       v-hasPermi="['operate:message:reply']">回复</el-button>

            <el-button v-if="scope.row.messageStatus !='3'"
                       size="mini"
                       type="text"
                       icon="el-icon-edit"
                       @click="handleEnd(scope.row)"
                       v-hasPermi="['operate:message:end']">结束</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total>0"
                  :total="total"
                  :page.sync="queryParams.pageNum"
                  :limit.sync="queryParams.pageSize"
                  @pagination="getList" />
    </div>

    <!-- 添加或修改留言对话框 -->
    <div v-if="open && !isView"
         class="editdata">
      <p class="back"><span class="el-icon-arrow-left"
              @click="goBack">返回</span> / <span>{{title}}</span></p>

      <el-form ref="form"
               :model="form"
               :rules="rules"
               label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="留言标题"
                          prop="messageTitle">
              <el-input v-model="form.messageTitle"
                        placeholder="请输入留言标题" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="留言类型"
                          prop="messageType">
              <el-select v-model="form.messageType"
                         placeholder="请选择留言类型">
                <el-option v-for="dict in dict.type.sys_message_type"
                           :key="dict.value"
                           :label="dict.label"
                           :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="20">
            <el-form-item label="详细内容">
              <el-input type="textarea" :rows="12" v-model="form.messageContent"></el-input>
            </el-form-item>

            <el-form-item label="附件">
              <DragUpload1 v-model="form.messageFileList"
                           :data="form.messageFileList"
                           :min-height="192" />
            </el-form-item>
          </el-col>
        </el-row>

      </el-form>
      <div slot="footer"
           class="dialog-footer">
        <el-button type="primary"
                   @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </div>

    <!--查看-->
    <div v-if="isView"
         class="editdata">
      <p class="back"><span class="el-icon-arrow-left"
              @click="goBack">返回</span> / <span>查看</span></p>
      <el-form ref="form"
               :model="viewForm"
               :rules="rules"
               label-width="80px">
        <div>
          <el-row>
            <el-col :span="8">
              <el-form-item label="客户姓名">
                <el-input v-model="viewForm.sysUser.nickName"
                          readOnly />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="手机号">
                <el-input v-model="viewForm.sysUser.phonenumber"
                          readOnly />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="邮箱">
                <el-input v-model="viewForm.sysUser.email"
                          placeholder="请输入留言标题"
                          readOnly />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="8">
              <el-form-item label="留言标题">
                <el-input v-model="viewForm.messageTitle"
                          readOnly />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="留言类型">
                <el-input v-model="viewForm.messageTypeName"
                          readOnly />
              </el-form-item>
            </el-col>

            <el-col :span="8">
              <el-form-item label="留言时间">
                <el-input v-model="viewForm.messageTime"
                          readOnly />
              </el-form-item>
            </el-col>

          </el-row>

          <el-row>
            <el-col :span="20">
              <el-form-item label="详细内容">
                <el-input type="textarea" :rows="12" readonly v-model="viewForm.messageContent"></el-input>

              </el-form-item>

            </el-col>
          </el-row>

          <el-row>
            <el-col :span="20">
              <el-form-item label="附件">
                <el-row :span="8"
                        v-for="(item,index) of viewForm.messageFileList"
                        :key="index">
                  <el-button @click="downloadMessageFile(item.fileUrl,item.fileName)"
                             type="text"
                             class="text">{{item.fileName}}</el-button>
                </el-row>

              </el-form-item>

            </el-col>
          </el-row>

          <el-row>
            <el-col :span="20">
              <el-form-item label="状态">
                <el-input v-model="viewForm.messageStatusName"
                          readOnly />
              </el-form-item>
            </el-col>
          </el-row>
        </div>
        <div style="width: 100%;border-bottom: 1px solid #979797">
        </div>
        <div style="margin-top: 20px">
          <el-row>
            <el-col :span="20"
                    v-for="(item,index) of viewForm.sysReplyList"
                    :key="item.id">
              <el-form-item label="后台回复"
                            v-if="item.replyType == 1">
                <el-input type="textarea" :rows="8" readonly v-model="item.replyContent"></el-input>
              </el-form-item>

              <el-form-item label="用户回复"
                            v-if="item.replyType == 2">
                <editor api-key="4ed6kvrs1pq3f86i1z9bau4jjbrbr71e642vkzjty2zqxiqd" v-model="item.replyContent"
                        :min-height="192"
                        readOnly />
              </el-form-item>
              <el-row>
                <el-col :span="20"
                        v-if="item.replyFileList">
                  <el-form-item label="附件">
                    <el-row :span="8"
                            v-for="(v) of item.replyFileList"
                            :key="v.id">
                      <el-button @click="downloadMessageFile(v.fileUrl,v.fileName)"
                                 type="text"
                                 class="text">{{v.fileName}}</el-button>
                    </el-row>

                  </el-form-item>

                </el-col>
              </el-row>
              <el-form-item label="回复时间">
                <el-input v-model="item.replyTime"
                          :min-height="192"
                          readOnly />
              </el-form-item>

            </el-col>
          </el-row>
        </div>
      </el-form>
      <div v-if="viewForm.messageStatus != 3"
           slot="footer"
           class="dialog-footer"
           style="float: right">
      </div>
    </div>

    <!-- 回复对话框    -->
    <el-dialog :title="replyTitle"
               :visible.sync="replyOpen"
               width="780px"
               append-to-body>
      <el-form ref="replyForm"
               :model="replyForm"
               :rules="replyRules"
               label-width="80px">
        <el-row>

          <el-col :span="20">
            <el-form-item label="回复内容"
                          prop="replyContent">
              <el-input type="textarea" :rows="12" v-model="replyForm.replyContent"></el-input>

            </el-form-item>
            <el-form-item label="附件">
              <DragUpload v-model="replyForm.fileId"
                          :min-height="192" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer"
           class="dialog-footer">
        <el-button type="primary"
                   @click="replySubmitForm">确 定</el-button>
        <el-button @click="replyCancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { listMessage, getMessage, delMessage, addMessage, updateMessage, replyMessage, endMessage, downloadMessageFile } from "@/api/operate/message";
import DragUpload from "@/components/DragUpload";
import DragUpload1 from "@/components/DragUpload/index1.vue";

export default {
  components: {
    DragUpload,
    DragUpload1,
  },

  name: "Message",
  dicts: ['sys_message_status', 'sys_message_type'],
  data () {
    return {
      isRegister:undefined,
      role : undefined,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 留言表格数据
      messageList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      isView: false,//是否查看页面
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        messageType:undefined,
        messageTitle: undefined,
        messageStatus: undefined
      },
      // 表单参数
      form: {
        file: {}
      },

      replyForm: {
        file: {}
      },

      viewForm: {

      },

      // 弹出层标题
      replyTitle: "",
      // 是否显示弹出层
      replyOpen: false,

      // 表单校验
      rules: {
        messageTitle: [
          { required: true, message: "留言标题不能为空", trigger: "blur" }
        ],
        messageType: [
          { required: true, message: "留言类型不能为空", trigger: "change" }
        ]
      },

      replyRules: {
        replyContent: [
          { required: true, message: "回复内容不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created () {
    var that = this;
    var isRegister = this.$store.state.user.isRegister;
    var roles = this.$store.state.user.roles;
    if(roles && roles.length > 0){
        //遍历找到admin
      for(let i = 0 ; i < roles.length; i ++){
          if(roles[i] == 'admin'){
            that.role = roles[i];
          }
      }
    }
    that.isRegister = isRegister;
    that.getList();
  },
  mounted(){
    if(this.$route.query.id){
      this.handleUpdate(this.$route.query)
    }
  },
  methods: {
    goBack () {
      this.open = false
      this.isView = false
    },
    replyReset () {
      this.replyForm = {
        messageId: undefined,
        replyContent: undefined,
        fileId:undefined
      };
      this.resetForm("replyForm");
      this.replyOpen = false
      this.replyTitle = "回复"
    },

    handleEnd1 () {
      const id = this.replyForm.messageId;
      var that = this;
      this.$modal.confirm('当前留言内容确定结束，可以关闭留言了么？').then(function () {
        return endMessage(id + "");
      }).then(() => {
        this.$modal.msgSuccess("结束成功");
        that.open = false;
        that.isView = false;
        this.getList();
      }).catch(() => { });
    },

    /**回复按钮*/
    handleReply1 () {
      this.replyOpen = true;
      this.replyTitle = "回复";
    },

    /** 查看按钮 */
    handleView (row) {
      this.reset();
      const id = row.id || this.ids
      this.replyForm.messageId = id;
      getMessage(id).then(response => {
        this.viewForm = response.data;
        this.isView = true;
        this.title = "查看留言";
      });
    },

    downloadMessageFile (url, fileName) {
      downloadMessageFile(url, fileName);
    },

    //回复取消按钮
    replyCancel () {
      this.replyOpen = false
    },


    /** 查询留言列表 */
    getList () {
      this.loading = true;
      listMessage(this.queryParams).then(response => {
        this.messageList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel () {
      this.open = false;
      this.isView = false
      this.reset();
    },
    // 表单重置
    reset () {
      this.form = {
        id: undefined,
        messageTitle: undefined,
        messageType: undefined,
        messageContent: undefined,
        messageStatus: "1",
      };
      this.resetForm("form");
    },


    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange (selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },

    /**回复按钮*/
    handleReply (row) {
      this.replyReset();
      const id = row.id || this.replyForm.messageId
      this.replyForm.messageId = id;
      this.replyOpen = true;
      this.replyTitle = "回复";
    },

    /** 新增按钮操作 */
    handleAdd () {
      this.reset();
      this.open = true;
      this.isView = false
      this.title = "添加留言";
    },
    /** 修改按钮操作 */
    handleUpdate (row) {
      this.reset();
      const id = row.id || this.ids
      getMessage(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.isView = false
        this.title = "修改留言";
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        var that = this;
        if (valid) {
          if (that.form.id != undefined) {
            if (that.form.messageFileList == undefined) {

            } else {
              const arr = []
              that.form.messageFileList.forEach(item => {
                arr.push(item.id)
              })
              that.form.fileId = JSON.stringify(arr);
              updateMessage(this.form).then(response => {
                that.$modal.msgSuccess("修改成功");
                that.open = false;
                that.isView = false
                that.getList();
              });
            }
          } else {
            const arr = []
            if (that.form.messageFileList == undefined) {

            }else{
              that.form.messageFileList.forEach(item => {
                arr.push(item.id)
              })
            }
            this.form.fileId = JSON.stringify(arr);
            addMessage(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.isView = false
              this.getList();
            });
          }
        }
      });
    },

    //回复确定按钮
    replySubmitForm: function () {
      this.$refs["replyForm"].validate(valid => {
        var that = this;
        if(valid){
          if (this.replyForm.messageId != undefined) {
            replyMessage(this.replyForm).then(response => {
              this.$modal.msgSuccess("回复成功");
              this.replyOpen = false;
              getMessage(this.replyForm.messageId).then(response => {
                this.viewForm = response.data;
                this.isView = true;
                this.title = "查看留言";
              });
              this.getList();
            });
          }
        }
      })
    },

    /** 删除按钮操作 */
    handleDelete (row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除留言编号为"' + ids + '"的数据项？').then(function () {
        return delMessage(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },

    /** 结束按钮操作 */
    handleEnd (row) {
      const id = row.id || this.ids
      this.$modal.confirm('当前留言内容确定结束，可以关闭留言了么？').then(function () {
        return endMessage(id + "");
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("结束成功");
      }).catch(() => { });
    }
  }
};
</script>

<style lang="scss" scoped>
.app-container {
  padding: 0;
  background-color: #ffffff;
  height: 100%;
  .body {
    padding: 20px;
    // height: 80vh;
    // overflow: auto;
  }
  .editdata {
    width: 100%;
    height: calc(100vh - 100px);
    overflow: auto;
    background: #ffffff;
    border-radius: 0px 0px 0px 0px;
    padding: 1px 20px;
    box-sizing: border-box;
    // 面包屑样式
    .back {
      .el-icon-arrow-left {
        color: #979797;
        cursor: pointer;
      }
      span {
        font-size: 14px;
        font-family: Source Code Pro-Regular, Source Code Pro;
        font-weight: 400;
        color: #1f2329;
        line-height: 22px;
      }
    }
    // form表单样式
    .editbox {
      padding: 20px;
      box-sizing: border-box;
      background: #ffffff;
      box-shadow: 0px 4px 12px 0px rgba(211, 211, 211, 0.25);
      border-radius: 6px 6px 6px 6px;
    }
    .editfooter {
      margin-top: 20px;
      > div {
        display: flex;
        padding-bottom: 44px;
        .footbox {
          width: 150px;
          height: 96px;
          background: #f7f8fa;
          border-radius: 4px 4px 4px 4px;
          display: flex;
          justify-content: center;
          align-items: center;
          margin-right: 20px;
          position: relative;
          cursor: pointer;
          img {
            width: 23px;
            height: 24px;
          }
          .icon_box {
            width: 16px;
            height: 16px;
            position: absolute;
            right: 10px;
            top: 10px;
          }
        }
      }
    }

    h3 {
      font-family: Source Code Pro-Bold, Source Code Pro;
      font-weight: bold;
      color: #1f2329;
      line-height: 24px;
      margin: 0 0 20px 0;
      padding: 0;
    }
  }

  // 取消保存审核公用样式----后有需要 可差离出去
  .btn {
    display: inline-block;
    width: 80px;
    height: 40px;
    text-align: center;
    line-height: 40px;
    margin-right: 20px;
    cursor: pointer;
  }
  .cancel {
    background: #e6ebf0;
    border-radius: 2px 2px 2px 2px;
  }
  .save {
    background: #0b9eae;
    color: #ffffff;
    border-radius: 2px 2px 2px 2px;
  }
  .examine {
    width: 161px;
    background: #aa2290;
    color: #ffffff;
    border-radius: 4px 4px 4px 4px;
  }
}
</style>
